/*
 * @author: xiaozhen
 * @Date: 2022-11-21 17:24:31
 * @LastEditTime: 2023-03-15 17:25:11
 */
import { defineStore } from 'pinia';
import { messageFn } from '../../utils';
import {
  getUserInfoApi,
  getUserChargingApi,
  getUserPreferentialApi,
  getWalletInvoiceApi,
  getUserBankRecordApi,
  getWalletInfoApi,
  getDailyInfoApi,
  getWalletPageApi,
  getWaterPageApi,
  getWithholdingRecordApi,
  getBankListApi,
} from '/@/api/comprehensive/userInfo';
import type { userStateType } from '/#/store/comprehensive/userInfo';
import { useLoading } from '/@/components/Loading';

const [openFullLoading, closeFullLoading] = useLoading({
  tip: '加载中...',
});

export const comprehensiveUserStore = defineStore({
  id: 'comprehensiveUser-state',
  state: (): userStateType => ({
    company: {}, //公司信息
    userInfo: {}, // 用户信息
    contactPerson: [], // 联系人信息
    waterMeter: [], // 水表信息
    userLoading: false, // 用户信息loading
    billingInfo: {}, // 发票信息
    billingLoading: false, // 发票信息loading
    chargingInfo: {}, // 计费信息
    chargingLoading: false, // 计费loading
    preferentialInfo: {}, // 优惠信息
    preferentialLoading: false, // 优惠loading
    payInfo: {
      bank: {},
      bankRecode: [],
      wallet: {},
      walletRecode: [],
    }, // 缴费信息
    PayLoading: false, // 缴费loading
    dailyInfo: [], // 日报信息
  }),
  actions: {
    /**
     * 刷新页面
     */
    async refresh(user_id: string) {
      this.getUserInfoAciton(user_id);
      this.getUserPreferentialAction(user_id);
      this.getChargingAciton(user_id);
    
      this.getWalletInfoAction(user_id);
      this.getWalletInvoiceAction(user_id);
    },

    /**
     * @description: 获取用户-联系人-水表信息
     */
    async getUserInfoAciton(user_id: string) {
      console.log(user_id,'user_id');
      
      this.userLoading = true;
      openFullLoading()
      try {
        const { code, msg, data } = await getUserInfoApi({ user_id });
        if (code === 0) {
          this.company = data.company;
          this.userInfo = data.user;
          this.userInfo.water_usage_name = data?.user_contact_master_vo[0]?.water_usage_name;
          this.userInfo.user_contact_master_vo = data?.user_contact_master_vo ?? [];
          this.contactPerson = data.user.contact_list;
          this.waterMeter = data.user.meter_info_list;
          this.waterMeter.forEach((item) => {
            item.reading_detail_vos?.slice(0, 2);
          });
          return data.user.contact_list;
        } else {
          this.userInfo = {};
          this.contactPerson = [];
          this.waterMeter = [];
          messageFn('error', msg);
        }
      } finally {
        this.userLoading = false;
        closeFullLoading()
      }
    },
    /**
     * @description: 获取计费信息
     */
    async getChargingAciton(user_id: string) {
      this.chargingLoading = true;
      try {
        const { code, msg, data } = await getUserChargingApi(user_id);
        if (code === 0) {
          this.chargingInfo = data;
        } else {
          this.chargingInfo = {};
          messageFn('error', msg);
        }
      } finally {
        this.chargingLoading = false;
      }
    },
    /**
     * @description: 获取日报信息
     */
    async getDailyInfoAction(params: object) {
      this.chargingLoading = true;
      try {
        const { code, msg, data } = await getDailyInfoApi(params);
        if (code === 0) {
          this.dailyInfo = data;
          this.dailyInfo.totalData = data.list.reduce(
            (total, item) => {
              total.cash_count = parseFloat(total.cash_count + item.cash_count);
              total.cash_total = parseFloat(total.cash_total + item.cash_total);
              total.pos_count = parseFloat(total.pos_count + item.pos_count);
              total.pos_total = parseFloat(total.pos_total + item.pos_total);
              total.total = parseFloat(total.total + item.total);
              return total;
            },
            {
              toll_collector_name: '合计',
              cash_count: 0,
              cash_total: 0,
              pos_count: 0,
              pos_total: 0,
              total: 0,
            },
          );
        } else {
          this.dailyInfo = [];
          messageFn('error', msg);
        }
      } finally {
        this.chargingLoading = false;
      }
    },
    /**
     * @description: 获取发票
     */
    async getWalletInvoiceAction(user_id: string) {
      this.billingLoading = true;
      try {
        const { code, msg, data } = await getWalletInvoiceApi({ user_id });
        if (code === 0) {
          this.billingInfo = data;
        } else {
          this.billingInfo = {};
          messageFn('error', msg);
        }
      } finally {
        this.billingLoading = false;
      }
    },
    /**
     * @description: 获得系统优惠方案使用账户列表
     */
    async getUserPreferentialAction(user_id: string) {
      this.preferentialLoading = true;
      try {
        const { code, msg, data } = await getUserPreferentialApi({ user_id });
        if (code === 0) {
          console.log(data, '账户列表');

          this.preferentialInfo = data;
        } else {
          this.preferentialInfo = {};
          messageFn('error', msg);
        }
      } finally {
        this.preferentialLoading = false;
      }
    },
    /**
     * @description: 获得获得用户钱包信息
     */
    async getWalletInfoAction(user_id: string) {
      this.PayLoading = true;
      try {
        const { code, msg, data } = await getWalletInfoApi({ user_id });
        if (code === 0) {
          this.payInfo.wallet = data;
          this.payInfo.walletRecode = data ? [data.detail] : [];
        } else {
          this.payInfo.wallet = {};
          this.payInfo.walletRecode = {};
          messageFn('error', msg);
        }
      } finally {
        this.PayLoading = false;
      }
    },
    /**
     * @description: 获得缴费信息
     */
    async getUserPayction(user_id: string) {
      this.PayLoading = true;
      try {
        const { code, data, msg } = await getUserBankRecordApi(user_id);
        if (code === 0) {
          this.payInfo.bank = data ? data.bank_contact_vo : {};
          this.payInfo.bankRecode = data?.payment_time ? [data] : [];
        } else {
          this.payInfo.bank = {};
          this.payInfo.bankRecode = [];
          messageFn('error', msg);
        }
      } finally {
        this.PayLoading = false;
      }
    },
    /**
     * @description: 获得钱包分页信息
     */
    async getWalletPageAction(params: object): Promise<any> {
      try {
        const { code, data, msg, total } = await getWalletPageApi(params);
        if (code === 0) {
          return {
            data,
            total,
          };
        } else {
          messageFn('error', msg);
        }
      } finally {
      }
    },
    /**
     * @description: 获得抄表信息
     */
    async getWaterPageAciton(params: object): Promise<any> {
      const { code, data, msg, total } = await getWaterPageApi(params);
      if (code === 0) {
        return {
          data,
          total,
        };
      } else {
        messageFn('error', msg);
      }
    },
    /**
     * @description: 获得抄表信息
     */
    async getWithholdingRecordAction(params: object): Promise<any> {
      const { code, data, msg } = await getWithholdingRecordApi(params);
      if (code === 0) {
        return {
          data: data.list,
          total: data.total,
        };
      } else {
        messageFn('error', msg);
      }
    },
    /**
     * @description: 获得抄表信息
     */
    async getBankListAciton(user_id: string): Promise<any> {
      const { code, data, msg } = await getBankListApi(user_id);
      if (code === 0) {
        return data;
      } else {
        messageFn('error', msg);
      }
    },
  },
});
